{include file="header/index" /}
<div class="newContentBox">

    <form class="layui-form newListPart" name="add_config" id="add_config" method="post" action="{:url('add_config')}">
        <div class="newPart">
            <!-- 添加配置 -->
            <div class="newTitleBox">
                <span class="layui-inline newListTitle">添加配置</span>
                <a class="newBtnDefault newBtnGray" onclick="returnBack(this)"> <i class="fa fa-mail-reply"></i>返回</a>
            </div>
            <div class="newContent">
                <div class="layui-form-item cover_layui_f_i">
                    <label class="layui-form-label cover_layui_f_l">配置分类：</label>
                    <div class="layui-input-block cover_layui_i_b">
                        <select class="cover_layui_s" name="classify" id="classify">
                            <option value="0">==请选择==</option>
                            <option value="1">结算积分</option>
                            <option value="2">续费券抵用积分</option>
                            <option value="3">续费券有效期</option>
                            <option value="4">其他</option>
                        </select>
                    </div>
                </div>
                <div class="layui-form-item cover_layui_f_i">
                    <label class="layui-form-label cover_layui_f_l">配置标题：</label>
                    <div class="layui-input-block cover_layui_i_b">
                        <input class="layui-input" name="title" placeholder="请添加配置标题"/>
                    </div>
                </div>
                <div class="layui-form-item cover_layui_f_i">
                    <label class="layui-form-label cover_layui_f_l">配置类型：</label>
                    <div class="layui-input-block cover_layui_i_b">
                        <select class="cover_layui_s" onchange="stype()" name="type" id="type">
                            <option value="0">==请选择==</option>
                            <option value="1">时刻生效</option>
                            <option value="2">有效时间</option>
                        </select>
                    </div>
                </div>
                <div class="layui-form-item cover_layui_f_i time stime">
                    <label class="layui-form-label cover_layui_f_l">开始时间：</label>
                    <div class="layui-input-block cover_layui_i_b">
                        <input class="layui-input newDate cover_date" id="datetimeStart" type="text" name="datetimeStart" placeholder="请选择开始时间"/>
                        <span>开始时间为空，则从0开始</span>
                    </div>
                </div>
                <div class="layui-form-item cover_layui_f_i time stime">
                    <label class="layui-form-label cover_layui_f_l">结束时间：</label>
                    <div class="layui-input-block cover_layui_i_b">
                        <input class="layui-input newDate cover_date" id="datetimeEnd" type="text" name="datetimeEnd" placeholder="请选择结束时间"/>
                        <span>结束时间为空，则永不结束</span>
                    </div>
                </div>
                <div class="layui-form-item cover_layui_f_i">
                    <label class="layui-form-label cover_layui_f_l">配置权限：</label>
                    <div class="layui-input-block cover_layui_i_b">
                        <select class="cover_layui_s" name="disabled" id="disabled">
                            <option value="0">==请选择==</option>
                            <option value="1">可修改</option>
                            <option value="2">不可修改</option>
                        </select>
                    </div>
                </div>
                <div class="layui-form-item cover_layui_f_i">
                    <label class="layui-form-label cover_layui_f_l">配置格式：</label>
                    <div class="layui-input-block cover_layui_i_b">
                        <select class="cover_layui_s" onchange="c_format()" name="format" id="format">
                            <option value="0">==请选择==</option>
                            <option value="1">文本</option>
                            <option value="2">json</option>
                        </select>
                    </div>
                    <span>单项选择文本，多项选择json</span>
                </div>
                <div class="layui-form-item cover_layui_f_i">
                    <label class="layui-form-label cover_layui_f_l">配置键：</label>
                    <div class="layui-input-block cover_layui_i_b">
                        <input class="layui-input cover_date" id="key" type="text" name="key" placeholder="请添加配置键" />
                    </div>
                </div>
                <div class="layui-form-item cover_layui_f_i format format1">
                    <label class="layui-form-label cover_layui_f_l">配置值：</label>
                    <div class="layui-input-block cover_layui_i_b">
                        <input class="layui-input cover_date" id="value" type="text" name="value" placeholder="请添加配置值"/>
                    </div>
                </div>
                <div class="layui-form-item cover_layui_f_i json-format json-format1">
                    <label class="layui-form-label cover_layui_f_l">配置值：</label>
                    <div class="layui-input-block cover_layui_i_b">
                        <div class="c_value_arr">
                            <div class="c_value">
                                <input class="layui-input cover_date" type="text"  name="json_note" placeholder="请添加配置项标题"/>
                                <input class="layui-input cover_date" type="text"  name="json_key" placeholder="请添加配置key"/>
                                <input class="layui-input cover_date" type="text"  name="json_value" placeholder="请添加配置value"/>
                                <a class="layui-btn newBtnDefault" onclick="addValue()">添加</a>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="layui-form-item cover_layui_f_i">
                    <label class="layui-form-label cover_layui_f_l">配置状态：</label>
                    <div class="layui-input-block cover_layui_i_b">
                        <select class="cover_layui_s" name="state" id="state">
                            <option value="0">==请选择==</option>
                            <option value="1">开启</option>
                            <option value="2">关闭</option>
                        </select>
                    </div>
                </div>
                <div class="layui-form-item cover_layui_f_i">
                    <label class="layui-form-label cover_layui_f_l">配置说明：</label>
                    <div class="layui-input-block cover_layui_i_b">
                        <textarea class="layui-textarea cover_date" type="text" rows="5" name="note" id="extra"></textarea>
                    </div>
                </div>
            </div>
            <div class="layui-form-item ">
                <div class="layui-input-block cover_layui_i_b">
                    <a class="layui-btn newBtnDefault" onclick="javascript:history.go(-1);">取消</a>
                    <a class="layui-btn newBtnDefault newBtnPrimary" onclick="saveData()">保存</a>
                </div>
            </div>
        </div>

    </form>

</div>
<script type="text/javascript" src="__JS__/jquery.form.js"></script>
<script>
    var layer = layui.layer,
        element = layui.element,
        laydate = layui.laydate,
        laypage = layui.laypage,
        form = layui.form,
        laytpl = layui.laytpl;
    $.ajaxSetup({headers: {token: $.cookie('token'),company: $.cookie('company_id')}});;

    // 保存
    function saveData() {
        var classify = $("select[name='classify']").val();
        var type = $("select[name='type']").val();
        var disabled = $("select[name='disabled']").val();
        var format = $("select[name='format']").val();
        var state = $("select[name='state']").val();
        var title = $("input[name='title']").val();
        var datetimeStart = $("input[name='datetimeStart']").val();
        var datetimeEnd = $("input[name='datetimeEnd']").val();
        var key = $("input[name='key']").val();
        var value = $("input[name='value']").val();
        var id = $("input[name='id']").val();
        var note = $("#extra").val();

        if (classify == 0){
            layer.msg('请选择配置分类',{icon:2,time:1500,shade: 0.1,});
            return false;
        }
        if (title.length == 0){
            layer.msg('请输入配置标题',{icon:2,time:1500,shade: 0.1,});
            return false;
        }
        if (type == 0){
            layer.msg('请选择配置类型',{icon:2,time:1500,shade: 0.1,});
            return false;
        }
        if (disabled == 0){
            layer.msg('请选择配置权限',{icon:2,time:1500,shade: 0.1,});
            return false;
        }
        if (format == 0){
            layer.msg('请选择配置格式',{icon:2,time:1500,shade: 0.1,});
            return false;
        }
        if (key.length == 0){
            layer.msg('请选择配置键',{icon:2,time:1500,shade: 0.1,});
            return false;
        }
        if (format == 1 && value.length == 0){
            layer.msg('请选择配置值',{icon:2,time:1500,shade: 0.1,});
            return false;
        }

        if (format == 2){
            var key_length = $('input[name="json_key"]').length;
            var value_length = $('input[name="json_value"]').length;
            if (key_length != value_length){
                layer.msg('key和value数量不一致',{icon:2,time:1500,shade: 0.1,});
                return false;
            }
            var key_arr =[];
            $("input[name='json_key']").each(function(){
                if ($(this).val().length == 0){
                    layer.msg('请完成key的设置',{icon:2,time:1500,shade: 0.1,});
                    return false;
                }
                key_arr.push($(this).val());
            });
            if (key_length != key_arr.length){
                layer.msg('请完成key的设置',{icon:2,time:1500,shade: 0.1,});
                return false;
            }
            var value_arr =[];
            $("input[name='json_value']").each(function(){
                if ($(this).val().length == 0){
                    layer.msg('请完成value的设置',{icon:2,time:1500,shade: 0.1,});
                    return false;
                }
                value_arr.push($(this).val());
            });
            if (key_length != value_arr.length){
                layer.msg('请完成value的设置',{icon:2,time:1500,shade: 0.1,});
                return false;
            }
            if (key_arr.length != value_arr.length){
                layer.msg('请完善配置值',{icon:2,time:1500,shade: 0.1,});
                return false;
            }
            var valueData = {};
            for (var i = 0;i < key_arr.length;i ++){
                valueData[key_arr[i]] = value_arr[i];
            }
            value = valueData;
        }
        var value_note_arr =[];
        $("input[name='json_note']").each(function(){
            value_note_arr.push($(this).val());
        });
        if (state == 0){
            layer.msg('请选择配置状态',{icon:2,time:1500,shade: 0.1,});
            return false;
        }

        var index = layer.load(1, {
            shade: [0.1,'#fff'] //0.1透明度的白色背景
        });
        var data = {};
        data.id = id;
        data.classify = classify;
        data.disabled = disabled;
        data.type = type;
        data.title = title;
        data.format = format;
        data.key = key;
        data.value = value;
        data.value_note = value_note_arr;
        data.state = state;
        data.note = note;
        data.datetimeStart = datetimeStart;
        data.datetimeEnd = datetimeEnd;
        data = JSON.stringify(data);
        $.post("{:config('api')}/Achievement/Config/addConfig",data,function (flag) {
            layer.close(index);
            if (flag.code < 0){
                layer.msg(flag.message,{icon:2,time:1500,shade: 0.1,});
                return false;
            }
            layer.msg(flag.message,{icon:1,time:1500,shade: 0.1,});
            window.location.href = "{:url('Configure/index')}";
        });
    }

    // 配置格式
    function c_format() {
        var val = $("select[name='format']").val();
        if (val == 1){
            $(".format1").removeClass('format');
            $(".json-format1").addClass('json-format');
        } else {
            $(".format1").addClass('format');
            $(".json-format1").removeClass('json-format');
        }
    }
    // 删除配置值
    function delValue(obj) {
        $(obj).parent().remove();
    }
    // 添加配置值
    function addValue() {
        var html = '';
        html += '<div class="c_value">';
        html += '<input type="text" class="layui-input cover_date" name="json_note" placeholder="请添加配置项标题" />';
        html += '<input type="text" class="layui-input cover_date" name="json_key" placeholder="请添加配置key" />';
        html += '<input type="text" class="layui-input cover_date" name="json_value" placeholder="请添加配置value" />';
        html += '<a class="layui-btn newBtnDefault" onclick="delValue(this)">删除</a>';
        html += '</div>';
        $(".c_value_arr").append(html)
    }
    // 配置类型
    function stype(){
        var val = $("select[name='type']").val();
        if (val == 2){
            $(".stime").removeClass('time');
        } else {
            $(".stime").addClass('time');
        }
    }

    laydate.render({
        elem: '#datetimeStart'
        ,type: 'date'
        ,done: function(value, date, endDate){

        }
    });
    laydate.render({
        elem: '#datetimeEnd'
        ,type: 'date'
        ,done: function(value, date, endDate){

        }
    });

    //提交
    /*$(function(){
        $('#add_config').ajaxForm({
            beforeSubmit: checkForm,
            success: complete,
            dataType: 'json'
        });

        function checkForm(){
            if( '' == $.trim($('#name').val())){
                layer.msg('请输入配置标识',{icon:2,time:1500,shade: 0.1}, function(index){
                    layer.close(index);
                });
                return false;
            }
            if( '' == $.trim($('#value').val())){
                layer.msg('请输入配置值',{icon:2,time:1500,shade: 0.1}, function(index){
                    layer.close(index);
                });
                return false;
            }

        }

        function complete(data){
            if(data.code==1){
                layer.msg(data.msg, {icon: 1,time:1500,shade: 0.1}, function(index){
                    window.location.href="{:url('config/index')}";
                });
            }else{
                layer.msg(data.msg, {icon: 2,time:1500,shade: 0.1});
                return false;
            }
        }

    });*/
</script>
